
# -*- coding: utf-8 -*-
import scrapy

from dirbot.items import UgItem


class UgSpider(scrapy.Spider):
    name = "ug"
    allowed_domains = ["24meinv.me"]
    start_urls = (
        'http://m.24meinv.me/2013/12-10/tuimo13936_1.html',

    )


    def printLog(self, message):
        print('>>>>>>>>' + message)

    def parse(self, response):

        self.printLog("start.")

        next_page_url = response.xpath("//*[@id='next']/text()").extract()[0]

        self.printLog(next_page_url)
        img_url = response.xpath("//*[@id='bimgs']")
        self.printLog(img_url.extract()[0])
        item = UgItem()
        item['src'] = img_url.xpath('@src').extract()
        item['image_urls'] = item['src']
        item['title'] = img_url.xpath('@alt').extract()[0] + item['src'].split('/')[-1].split('.')

        self.printLog('yield item.')
        yield item

        self.printLog('yield resquest.')
        yield scrapy.Request('http://m.24meinv.me/2016/6-17/' + next_page_url, callback=self.parse)


        self.printLog("end.")

